import api from '@/service/api.js'
import router from '@/router'
import store from '@/store'

// export const login = async (data) => {
//   try {
//     const userRes = await api.users.login( "671619141952354534",  "671619141952354534")
//     // const userRes = await api.users.login(data.device_id, data.device_id)
//     localStorage.setItem("access_token", userRes.data.data.token.access_token);
//     localStorage.setItem("refresh_token", userRes.data.data.token.refresh_token);
//     localStorage.setItem("expires_at", userRes.data.data.token.expires_at);
//     localStorage.setItem("user", JSON.stringify(userRes.data.data.user));
//     store.commit("SET_USER", userRes.data.data.user);
//     store.commit("SET_ACCESS_TOKEN", userRes.data.data.token.access_token);
//     store.commit("SET_REFRESH_TOKEN", userRes.data.data.token.refresh_token);

//     const versionRes = await api.sys.getVersion();
//     if (versionRes.data.success == 200) {
//       localStorage.setItem("version", versionRes.data.data.current_version);
//     }
//     router.push("/");
//   } catch (err) {

//     console.log(err)
//   }
// }

export const logined = async (userRes) => {
  localStorage.setItem("access_token", userRes.data.token.access_token);
  localStorage.setItem("refresh_token", userRes.data.token.refresh_token);
  localStorage.setItem("expires_at", userRes.data.token.expires_at);
  localStorage.setItem("port", userRes.port);
  localStorage.setItem("user", JSON.stringify(userRes.data.user));
  store.commit("SET_USER", userRes.data.user);
  store.commit("SET_ACCESS_TOKEN", userRes.data.token.access_token);
  store.commit("SET_REFRESH_TOKEN", userRes.data.token.refresh_token);
  const versionRes = await api.sys.getVersion();
  if (versionRes.data.success == 200) {
    localStorage.setItem("version", versionRes.data.data.current_version);
  }
  router.push("/");
}